package cn.tpshion.devops.controller;

import cn.tpshion.devops.common.Result;
import cn.tpshion.devops.handle.SSEHandle;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import javax.annotation.Resource;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/sse")
public class SSEController {

    @Resource
    private SSEHandle sseHandle;

    @GetMapping(value = "/conn", produces = {MediaType.TEXT_EVENT_STREAM_VALUE})
    public SseEmitter connect(@RequestParam String taskId, @RequestParam String id){
        return sseHandle.connect(taskId, id);
    }

    @GetMapping("/close")
    public Result close(@RequestParam String taskId, @RequestParam String id){
        sseHandle.closeConnect(taskId, id);
        return Result.ok();
    }
}
